package com.changyue.activity.history;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.changyue.activity.basic.BasicActivity;
import com.changyue.activity.history.entity.HistoryDataEntity;
import com.changyue.bleHelper.BLEConstant;
import com.changyue.temp.R;
import com.changyue.temp.databinding.ActivityInfoHistoryBinding;
import com.changyue.ui.DynamicLineChartManager;
import com.changyue.ui.MyMarkerView;
import com.changyue.util.EmptyUtil;
import com.changyue.util.FileOpUtil;
import com.z.fileselectorlib.FileSelectorSettings;
import com.z.fileselectorlib.Objects.FileInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class HistoryInfoActivity extends BasicActivity {
    ActivityInfoHistoryBinding binding;

    public static String KEY_objectMap = "objectMap";

    private MyMarkerView mMv;
    public HistoryDataEntity historyDataEntity;

    private DynamicLineChartManager dynamicLineChartManager;
    private List<String> names = new ArrayList<>(); //折线名字集合
    private List<Integer> color = new ArrayList<>();//折线颜色集合

    public static void start(Context context, Long historyId) {
        Intent intent = new Intent(context, HistoryInfoActivity.class);
        intent.putExtra(KEY_objectMap, historyId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityInfoHistoryBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Long histroyId = getIntent().getLongExtra(KEY_objectMap, -1);
        historyDataEntity = DataManager.getHistory(histroyId);
        if (EmptyUtil.isNotEmpty(historyDataEntity)) {
            String workMethod = BLEConstant.get_WORK_METHOD_swv_cmd_name(historyDataEntity.workMethod);
            setTitle(historyDataEntity.save_date + " " + workMethod);
        }

        binding.rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_line:
                        binding.lineChart.setVisibility(View.VISIBLE);
                        binding.dataList.setVisibility(View.GONE);
                        binding.dataListHeader.setVisibility(View.GONE);
                        break;

                    case R.id.rb_data:
                        binding.lineChart.setVisibility(View.GONE);
                        binding.dataList.setVisibility(View.VISIBLE);
                        binding.dataListHeader.setVisibility(View.VISIBLE);
                        break;

                    default:

                        break;
                }
            }
        });

        mMv = new MyMarkerView(this, R.layout.marker_view);
        mMv.setChartView(binding.lineChart);
        binding.lineChart.setMarker(mMv);
        names.add("CH1");
        names.add("CH2");

        color.add(Color.RED);
        color.add(Color.BLUE);

        dynamicLineChartManager = new DynamicLineChartManager(binding.lineChart, names, color);

        if (EmptyUtil.isNotEmpty(historyDataEntity)) {
            dynamicLineChartManager.addALLEntry(historyDataEntity.getLineDatas());
            binding.lineChart.setDrawMarkers(true);
            binding.textCh1.setText(historyDataEntity.getResultInfo(this));
        }
    }

    private void exportData() {
        FileSelectorSettings settings = new FileSelectorSettings();
        settings.setRootPath(FileSelectorSettings.getSystemRootPath() + "/Download")//起始路径
                .setMaxFileSelect(1)//最大文件选择数
                .setTitle("请选择导出文件夹")//标题
                .setFileTypesToSelect(FileInfo.FileType.Folder)//可选择文件类型
                .show(HistoryInfoActivity.this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == FileSelectorSettings.FILE_LIST_REQUEST_CODE && resultCode == FileSelectorSettings.BACK_WITH_SELECTIONS) {
            assert data != null;
            Bundle bundle = data.getExtras();
            assert bundle != null;
            ArrayList<String> FilePathSelected = bundle.getStringArrayList(FileSelectorSettings.FILE_PATH_LIST_REQUEST);
            if (EmptyUtil.isEmpty(FilePathSelected)) {
                Toast.makeText(HistoryInfoActivity.this, "请选择文件", Toast.LENGTH_LONG).show();
            } else {
                String path = FilePathSelected.get(0);
                alertInputDialog(path);
            }
        }
    }
    public void alertInputDialog(String path) {

        final EditText editText = new EditText(HistoryInfoActivity.this);
        editText.setHint("请输入文件名称");
        editText.setBackground(null);
        editText.setMaxLines(1);
        android.app.AlertDialog.Builder singleChoiceDialog = new android.app.AlertDialog.Builder(HistoryInfoActivity.this);
        singleChoiceDialog.setTitle("提示");
        singleChoiceDialog.setView(editText);
        singleChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String txt = editText.getText().toString();
                        if (EmptyUtil.isEmpty(txt)) {
                            Toast.makeText(HistoryInfoActivity.this, "请输入文件名", Toast.LENGTH_LONG).show();
                            return;
                        }

                        if (txt.length() < 3) {
                            Toast.makeText(HistoryInfoActivity.this, "文件名需要大于3个字符", Toast.LENGTH_LONG).show();
                            return;
                        }

                        String FileName = txt + ".csv";
                        FileOpUtil.write2File(path + File.separator + FileName, historyDataEntity.getCSVString(HistoryInfoActivity.this), true);
                        Toast.makeText(HistoryInfoActivity.this, "导出成功", Toast.LENGTH_LONG).show();
                    }
                });
        singleChoiceDialog.setNegativeButton("取消", null);
        singleChoiceDialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.histroy_info_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.export_data: {
                exportData();
            }
            break;

            default:

                break;
        }
        return true;
    }
}
